package com.example.mygrade.webapi;


import com.example.mygrade.core.RUtil;
import com.example.mygrade.domain.Student;
import com.example.mygrade.domain.Teacher;
import com.example.mygrade.domain.TeacherMa;
import com.example.mygrade.exception.REnum;
import com.example.mygrade.intercepter.GlobalConstant;
import com.example.mygrade.service.StudentService;
import com.example.mygrade.service.TeacherMaService;
import com.example.mygrade.service.TeacherService;
import com.example.mygrade.vo.R;
import jdk.nashorn.internal.runtime.GlobalConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Objects;


@Slf4j
@RestController
@RequestMapping("/webapi/login")
public class LoginRestController {
    @Autowired
    TeacherMaService teacherMaService;

    @Autowired
    StudentService studentService;

    @Autowired
    TeacherService teacherService;

    /**
     * 返回的是统一返回值
     * @param httprequest
     * @param username
     * @param password
     * @return
     * @throws Exception
     */

    @GetMapping("/user")
    public R user(HttpServletRequest httprequest, @Validated String username,@Validated String password,String code) throws Exception{


        // 获取用户名和密码是否存在
        List<Student> student = studentService.findBySnameAndPassword(username,password);
        HttpSession  session = httprequest.getSession();

        String code1 = httprequest.getSession().getAttribute("VerifyCode").toString();
        if (!Objects.equals(code, code1)){
            return RUtil.error(400,"验证码不匹配");
        }else{
            // 设置当前的信息
            if (student.size() > 0){
                // 获取到了相关信息之后设置session
                session.setAttribute(GlobalConstant.USER_SESSION_ID.getMsg(), student.get(0).getId());
                session.setAttribute(GlobalConstant.TEACHER_SESSION_ID.getMsg(), null);
                session.setAttribute(GlobalConstant.MANAGE_SESSION_ID.getMsg(), null);
                session.setAttribute(GlobalConstant.USER_SESSION_KEY.getMsg(), student.get(0));
                return RUtil.success();
            }else{
                List<Teacher> teachers = teacherService.findByTnameAndPassword(username,password);
                if(teachers.size() > 0){
                    // 获取到了相关信息之后设置session
                    session.setAttribute(GlobalConstant.TEACHER_SESSION_ID.getMsg(), teachers.get(0).getId());
                    session.setAttribute(GlobalConstant.USER_SESSION_ID.getMsg(), null);
                    session.setAttribute(GlobalConstant.MANAGE_SESSION_ID.getMsg(), null);
                    session.setAttribute(GlobalConstant.TEACHER_SESSION_KEY.getMsg(), teachers.get(0));
                    return RUtil.success();
                }
                else {
                    List<TeacherMa> teacherMas = teacherMaService.findByMnameAndMpass(username,password);
                    if (teacherMas.size() > 0){
                        // 获取到了相关信息之后设置session
                        session.setAttribute(GlobalConstant.MANAGE_SESSION_ID.getMsg(), teacherMas.get(0).getId());
                        session.setAttribute(GlobalConstant.USER_SESSION_ID.getMsg(), null);
                        session.setAttribute(GlobalConstant.TEACHER_SESSION_ID.getMsg(), null);
                        session.setAttribute(GlobalConstant.MANAGE_SESSION_KEY.getMsg(), teacherMas.get(0));
                        return RUtil.success();
                    } else{
                        return RUtil.error(REnum.FAILED);
                    }

                }
            }
        }

    }
}
